package net.homelinux.chaoswg.io.renga.core.gui.operations;

import java.util.Observable;
import java.util.Observer;

import javax.swing.JOptionPane;

import net.homelinux.chaoswg.io.renga.core.gui.DrawingArea;
import net.homelinux.chaoswg.io.renga.core.gui.operations.helper.Connection;
import net.homelinux.chaoswg.io.renga.core.gui.operations.visualisations.NamedBlockVisualisation;
import net.homelinux.chaoswg.io.renga.core.registries.VariableRegistry;

public class VariableGetOperation extends Operation implements Observer {

    public VariableGetOperation () {
        this.visualisation = new NamedBlockVisualisation (0,0,32,32,"");

        this.inputs = 1;
        this.outputs = 1;

        this.connections = new Connection[1];
        this.connections[0] = new Connection();

        this.visualisation.setInputs(0);
        this.visualisation.setOutputs(1);

        connections[0].addObserver(this);
    }

    public void connectTo (final VariablePutOperation vput) {
        if (! (producesCircle(vput))) {
            this.connections[0].setInputOperation(vput);
            this.connections[0].setOutput(0);
            ((NamedBlockVisualisation)this.visualisation).setName(vput.getName());
        }
    }

    public void connect (final int i, final Operation otherEnd, final int output) {
        super.connect(i, otherEnd, output);

        ((NamedBlockVisualisation)this.visualisation).setName(((VariablePutOperation)otherEnd).getName());
    }


    @Override
    public Object apply(final int i) {
        if (i >= outputs) {
            return null;
        }
        result = connections[i].getResult();

        return result;
    }

    public void update(final Observable arg0, final Object arg1) {
        if (arg0 == this.visualisation) {
            //Nothing to be done here for the moment
        }

        if (arg0 == this.connections[0]) {
            Operation op = connections[0].getInputOperation();
            if (op != null) {
                ((NamedBlockVisualisation)this.visualisation).setName(op.toString());
            } else {
                ((NamedBlockVisualisation)this.visualisation).setName("");
            }
        }
    }

    @Override
    public String getDoubleClickActionName() {
        return "Select Input";
    }

    @Override
    public void doubleClick(final DrawingArea dv) {
        Object name = null;

        final Object [] possibleValues = VariableRegistry.getVariableRegistry().getPossible().toArray();

        if (possibleValues.length > 0) {
            name = JOptionPane.showInputDialog(dv, "Please select binding as input", 
                    "INPUT", JOptionPane.INFORMATION_MESSAGE, null, possibleValues, ((String)possibleValues[0]));
        } else {
            JOptionPane.showMessageDialog(dv, "Sorry you haven't specified any Binding-put-names so you can't select one", "Error", JOptionPane.ERROR_MESSAGE);
        }
        if (name == null) {
            this.reset();
        } else {
            this.connectTo(VariableRegistry.getVariableRegistry().getVariablePut(((String)name)));
        }

    }
}
